package CutLoad.web.slide
{
	import CutLoad.Base;
	import CutLoad.components.CButton;
	import CutLoad.components.CButtonBar;
	import CutLoad.cookies.CLoadImg;
	import CutLoad.cookies.CLoader;
	import CutLoad.cookies.LayoutTable;
	import CutLoad.cookies.Painter;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 幻灯片
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 下午01:52:18 
	 *
	 */
	public class CSlide extends CSlideBase
	{
		/**
		 * 每一个画面显示的帧数 
		 */		
		protected var arrDelay:Array;
		protected var spMask:Sprite;
		protected var spPreMask:Sprite;
		/**
		 * 自动滚换是否开始 
		 */		
		protected var isStart:Boolean;
		/**
		 * 用于保存，当前加载的对象是否加载完成 
		 */		
		protected var isCurrentLoadComplete:Boolean;
		/**
		 * 按钮组 
		 */		
		protected var btnBar:CButtonBar;
		/**
		 *  按钮组的布局对象
		 */		
		protected var LT_btnBar:LayoutTable;
		/**
		 * 用于保存前一个滚动画面 
		 */		
		protected var preDisplay:Base;		
		private var funTransition:Function;
		private var time_i:Number=0;
		private var showWidth:Number,showHeight:Number;
		/**
		 * 
		 * @param arrData
		 * @param arrDelay
		 * @param isStart
		 * 
		 */		
		public function CSlide(width:Number,height:Number,arrData:Array,arrDelay:Array,funTransition:Function=null,alignType:Number=5,isStart:Boolean=true)
		{
			this.showWidth=width;
			this.showHeight=height
			super(arrData);
			this.arrDelay=arrDelay;
			if(arrDelay.length<arrData.length){
				var tem:Number=arrData.length-arrDelay.length;
				for(var j:Number=0;j<tem;j++){
					arrDelay.push(arrDelay[arrDelay.length-1]);
				}
			}
			isCurrentLoadComplete=false;
			this.isStart=isStart;
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			//按钮组
			var arrBtn:Array=new Array();
			for(var i:Number=0;i<arrData.length;i++){
				var btn:CButton=getBtnBarOneBtn(i,arrData[i]);
				arrBtn.push(btn);
			}
			btnBar=new CButtonBar(arrBtn,btnBarFunChang,5);
			btnBar.show(this);
			LT_btnBar=LayoutTable.getOneTdLayoutTable(width,height);
			LT_btnBar.setXY(alignType,btnBar,0,0);
			//显示第一个
			showOne(0);
			//遮蔽
			spMask=Painter.getRect(width,height,0xff0000,.5);
			spPreMask=Painter.getRect(width,height,0xff0000,0);
			this.addChild(spMask);
			this.addChild(spPreMask);
			this.containerSlideItem.mask=spMask;
			//
			//
			if(funTransition==null) funTransition=default_funTransition;
			this.funTransition=funTransition;
		}
		override protected function getLoadImg(url:String):Base{
			return new CLoadImg(url,0,0,function(me:CLoadImg):void{
				isCurrentLoadComplete=true;
				time_i=0;
			});
		}
		override protected function getLoadSwf(url:String):Base{
			var cloader:CLoader=new CLoader(url);
			cloader.setFunCompelet(function(me:CLoader):void{
				isCurrentLoadComplete=true;
				time_i=0;
			});
			return cloader;
		}
		override public function killMe():void{
			super.killMe();
		}
		override public function showOne(index:uint):Boolean{
			isCurrentLoadComplete=false;
			time_i=0;
			start()
			return super.showOne(index);
		}
		override protected function showCurrentIndex():void{
			//改变按钮条当前选重状态
			btnBar.changCurrentSelectBtn(current_index);
			//保存准备去掉的显示对象
			preDisplay=this.currentDisplay;
			super.showCurrentIndex();
			//
			if(preDisplay!=null){
				funTransition(preDisplay,currentDisplay);
				if(preDisplay.parent!=null) preDisplay.parent.removeChild(preDisplay);
				this.addChild(this.preDisplay);
				preDisplay.mask=spPreMask;
				
			}
		}
		private function btnBarFunChang(index:Number,me:CButtonBar):void{
			showOne(index);
		}
		/**
		 * 生成按钮组里单个按钮里，从这里获得
		 * @return 
		 * 
		 */		
		protected function getBtnBarOneBtn(index:Number,url:String):CButton{
			//return CButton.getSimpleBtn("");
			return new CSlideBtn();
		}
		/**
		 * 默认的过渡效果，淡入淡出 
		 * @param currentDisplay
		 * @param nextDisplay
		 * 
		 */		
		protected function default_funTransition_1(currentDisplay:Base,nextDisplay:Base):void{
			TweenLite.to(currentDisplay, 1, {alpha:0,onComplete:currentDisplay_onComplete,onCompleteParams:[currentDisplay]});
			nextDisplay.alpha=0;
			TweenLite.to(nextDisplay, 1, {alpha:1});
		}
		private function currentDisplay_onComplete(cd:Base):void{
			cd.parent.removeChild(cd);
		}
		public function default_funTransition(currentDisplay:Base,nextDisplay:Base):void{
			TweenLite.to(currentDisplay, 1, {x:-currentDisplay.width,onComplete:currentDisplay_onComplete,onCompleteParams:[currentDisplay]});
			//nextDisplay.alpha=0;
			nextDisplay.x=spMask.width;
			nextDisplay.alpha=0;
			TweenLite.to(nextDisplay, 1, {x:0,alpha:1});
		}
		/**
		 * 循环操作 
		 * @param e
		 * 
		 */		
		protected function enterFrame(e:Event):void{
			//trace("isStart:"+isStart+" isCurrentLoadComplete:"+isCurrentLoadComplete);
			//trace("time_i:"+time_i+" this.arrDelay["+this.current_index+"]:"+this.arrDelay[this.current_index]);
			if(!isStart) return;
			if(isCurrentLoadComplete){
				if(time_i++>this.arrDelay[this.current_index]){
					time_i=0;
					this.next();
				}
			}
		}
		/**
		 * 开始轮换 
		 * 
		 */		
		public function start():void{
			isStart=true;
		}
		/**
		 * 停止轮换 
		 * 
		 */		
		public function stop():void{
			isStart=false;
		}
		override public function getShowWidth():Number{
			return this.showWidth
		}
		override public function getShowHeight():Number{
			return this.showHeight;
		}
	}
}